<template>
  <div id="goods">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="10">
      <!-- 搜索框 -->
      <el-col :span="8">
        <el-input
          placeholder="请输入搜索内容"
          v-model="goodsPagination.query"
          class="input-with-select"
          clearable
          @clear="getGoodsInfo"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getGoodsInfo"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <!-- 添加商品 -->
        <el-button type="primary" class="addInfo" @click="addGoods">添加商品</el-button>
      </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="getLists" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="95px"
        ></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <template #default="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              class="btnGoods"
              @click="editorGoods(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              class="btnGoods"
              @click="delGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodsPagination.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
      <!-- 编辑对话框 -->
      <el-dialog
        title="编辑商品信息"
        :visible.sync="editorDataVisible"
        width="50%"
      >
        <el-form ref="editorFormRef" :model="editorData" label-width="80px">
          <el-form-item label="商品名称">
            <el-input v-model="editorData.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格">
            <el-input v-model="editorData.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量">
            <el-input v-model="editorData.goods_weight"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editorDataVisible = false">取 消</el-button>
          <el-button type="primary" @click="editorDataVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'goods',
  data() {
    return {
      // 商品列表总数据
      getLists: [],
      // 商品列表数据分页
      goodsPagination: {
        // 查询参数用于搜索关键字
        query: '',
        // 当前第几页
        pagenum: 1,
        // 每页显示几条数据
        pagesize: 10,
      },
      // 商品总页数
      total: 0,
      // 编辑商品需要的数据
      editorData: {},
      // 弹出与关闭
      editorDataVisible: false,
    }
  },
  created() {
    // 调用商品列表总数据
    this.getGoodsInfo()
  },
  methods: {
    // 获取商品列表总数据
    async getGoodsInfo() {
      const { data: res } = await this.$http.get('goods', {
        params: this.goodsPagination,
      })
      if (res.meta.status !== 200) return
      this.getLists = res.data.goods
      this.total = res.data.total
      // console.log(this.getLists)
      // console.log(this.goodsPagination)
    },
    // 每页几条数据
    handleSizeChange(val) {
      // 把每页几条数据赋值 给 商品列表数据分页
      // 然后重新渲染数据
      this.goodsPagination.pagesize = val
      this.getGoodsInfo()
    },
    // 当前第几页
    handleCurrentChange(val) {
      // 把当前点击的第几页赋值 给 商品列表数据分页
      // 然后重新渲染数据
      this.goodsPagination.pagenum = val
      this.getGoodsInfo()
    },
    // 编辑表单
    async editorGoods(id) {
      const { data: res } = await this.$http.get(`goods/${id}`)
      if (res.meta.status !== 200) return
      this.editorData = res.data
      this.editorDataVisible = true
    },
    // 删除商品
    delGoods(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${id}`)
          if (res.meta.status !== 200) return
          this.$message.success('删除成功!')
          this.getGoodsInfo()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 添加商品
    addGoods(){
      this.$router.push('/goods/add')
    }
  },
}
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
  box-shadow: none !important;
}
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.el-table {
  margin-top: 15px;
  font-size: 12px;
}
.btnGoods {
  padding: 7px 15px;
  font-size: 12px;
}
.el-pagination {
  margin-top: 15px;
}
</style>